package com.shisfish.library.Utils.PageModel;

public class PageUtils {
	
	public static Page createPage(int totalCount, int currentPage, int pageSize){
		pageSize = getPageSize(pageSize);
		currentPage = getCurrentPage(currentPage);
		int totalPage = getTotalPage(pageSize, totalCount);
		int beginIndex = getBeginIndex(pageSize, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(totalPage, currentPage);
		return new Page(pageSize, totalCount, totalPage, currentPage, beginIndex, hasPrePage,  hasNextPage);
	}

	private static boolean getHasNextPage(int totalPage, int currentPage) {
		// TODO Auto-generated method stub
		return currentPage == totalPage || totalPage ==0 ? false : true;
	}

	private static boolean getHasPrePage(int currentPage) {
		// TODO Auto-generated method stub
		return currentPage == 1 ? false : true;
	}

	private static int getBeginIndex(int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return (currentPage - 1) * pageSize;
	}

	private static int getTotalPage(int pageSize, int totalCount) {
		// TODO Auto-generated method stub
		int totalPage = 0;
		if(totalCount!=0 && pageSize !=0){
			if(totalCount % pageSize == 0){
				totalPage = totalCount/pageSize;
			}
			else{
				totalPage = totalCount/pageSize + 1;
			}
		}
		return totalPage;
	}

	private static int getCurrentPage(int currentPage) {
		// TODO Auto-generated method stub
		// 当currentPage为0时， 默认当前页为第一页
		return currentPage == 0 ? 1 : currentPage;
	}

	private static int getPageSize(int pageSize) {
		// TODO Auto-generated method stub
		// 当pageSize为0时， 默认每页显示十条数据
		return pageSize == 0 ? 10 : pageSize;
	}
}
